﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HealthHelper
{

    public class ConfigJsonFactory
    {
        public static readonly string fileName = "app.json";
        public static ConfigJson LoadConfig()
        {
            ConfigJson configJson = new ConfigJson()
            {
                WorkMinutes = 45,
                RestMinutes = 3,
                RestRemindSecond = 15,
                MinWorkMinutes = 1,
                MaxWorkMinutes = 180
            };
            if (!File.Exists(fileName))
            {
                return configJson;
            }
            try
            {
                string json = File.ReadAllText(fileName);
                configJson = JsonConvert.DeserializeObject<ConfigJson>(json);
            }
            catch (Exception ex)
            {
                MessageBox.Show("配置文件加载异常：" + ex.Message);
            }
            return configJson;
        }


        public static void WriteConfig(ConfigJson configJson)
        {
            JsonSerializer serializer = new JsonSerializer();
            try
            {
                using (StringWriter textWriter = new StringWriter())
                {
                    using (JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                    {
                        Formatting = Formatting.Indented,
                        Indentation = 4,
                        IndentChar = ' '
                    })
                    {
                        serializer.Serialize(jsonWriter, configJson);
                        string json = textWriter.ToString();
                        File.WriteAllText(fileName, json);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("配置文件写异常：" + ex.Message);
            }
        }
    }

    public class ConfigJson
    {

        // 工作时长
        public int WorkMinutes { get; set; }

        // 休息时长
        public int RestMinutes { get; set; }


        // 开启休息标识
        public bool RestOpen { get; set; }


        // 休息提醒秒
        public int RestRemindSecond { get; set; }


        // 最小工作时间
        public int MinWorkMinutes { get; set; }

        // 最大工作时间
        public int MaxWorkMinutes { get; set; }
    }
}
